﻿Namespace FL_8x0.Base
    Public Class PasswordBoxHelper
        Public Shared ReadOnly _
            PasswordProperty As DependencyProperty = DependencyProperty.RegisterAttached("Password", GetType(String),
                                                                                         GetType(PasswordBoxHelper),
                                                                                         New PropertyMetadata(
                                                                                             New PropertyChangedCallback(
                                                                                                 AddressOf OnPropertyChanged)))

        Public Shared Function GetPassword(ByVal d As DependencyObject) As String
            Return CStr(d.GetValue(PasswordProperty))
        End Function

        Public Shared Sub SetPassword(ByVal d As DependencyObject, ByVal value As String)
            d.SetValue(PasswordProperty, value)
        End Sub

        Public Shared ReadOnly _
            AttachProperty As DependencyProperty = DependencyProperty.RegisterAttached("Attach", GetType(String),
                                                                                       GetType(PasswordBoxHelper),
                                                                                       New PropertyMetadata(
                                                                                           New PropertyChangedCallback(
                                                                                               AddressOf OnAttachChanged)))

        Public Shared Function GetAttach(ByVal d As DependencyObject) As String
            Return CStr(d.GetValue(PasswordProperty))
        End Function

        Public Shared Sub SetAttach(ByVal d As DependencyObject, ByVal value As String)
            d.SetValue(PasswordProperty, value)
        End Sub

        Private Shared _isUpdating As Boolean = False

        Private Shared Sub OnPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            Dim pb As PasswordBox = TryCast(d, PasswordBox)
            RemoveHandler pb.PasswordChanged, AddressOf Pb_PasswordChanged
            If Not _isUpdating Then
                TryCast(d, PasswordBox).Password = e.NewValue.ToString()
            End If
            AddHandler pb.PasswordChanged, AddressOf Pb_PasswordChanged
        End Sub

        Private Shared Sub OnAttachChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            Dim pb As PasswordBox = TryCast(d, PasswordBox)
            AddHandler pb.PasswordChanged, AddressOf Pb_PasswordChanged
        End Sub

        Private Shared Sub Pb_PasswordChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim pb As PasswordBox = (TryCast(sender, PasswordBox))
            _isUpdating = True
            SetPassword(pb, pb.Password)
            _isUpdating = False
        End Sub
    End Class
End Namespace